# -*- coding: utf-8 -*-
"""
Created on Mon Jan  8 16:50:36 2018

@author: HXP
"""

import json

text = '''
{
  "content": {
    "result": {
      "sid": "cid6f1c2241@ch00a90db371d801000a",
      "intent": {
        "sid": "cid6f1c2241@ch00a90db371d801000a",
        "text": "头动一下",
        "intentType": "custom",
        "vendor": "YUJIAO",
        "query": "头动一下",
        "state": {
          "fg::musicX::default::default": {
            "insType": "1"
          }
        },
        "semantic": [
          {
            "score": 1,
            "entrypoint": "ent",
            "intent": "general_action",
            "slots": [
              {
"begin": 0,
"end": 1,
"name": "position",
"normValue": "头",
"value": "头"
},
{
"begin": 1,
"end": 2,
"name": "position",
"normValue": "嘴",
"value": "嘴"
},
{
"begin": 2,
"end": 4,
"name": "position",
"normValue": "眼睛",
"value": "眼睛"
}
            ]
          }
        ],
        "cid": "cid6f1c2241@ch00a90db36f8e000000",
        "rc": 0,
        "version": "2.0",
        "category": "YUJIAO.robot_action",
        "nlis": "true",
        "query_ws": "头/AA//  动/VI//  一/NM//  下/ADD//",
        "service": "YUJIAO.robot_action",
        "uuid": "atn001771de@ch11710db371d96f2001"
      }
    },
    "arg2": 0,
    "eventType": 1,
    "arg1": 0,
    "info": {
      "data": [
        {
          "content": [
            {
              "dtf": "json",
              "cnt_id": "0",
              "dte": "utf8"
            }
          ],
          "params": {
            "rstid": 1,
            "sub": "nlp",
            "lrst": false
          }
        }
      ]
    }
  },
  "type": "aiui_event"
}
'''


# 解析动作意图，返回部位列表
def parse_action(json_str):
    print('json_len=%d' % len(json_str))
    if len(json_str) > 500:
        json_result = json.loads(json_str, encoding='utf-8')
        if 'content' in json_result:
            content = json_result['content']
            # 如果是结果事件
            if content['eventType'] == 1:
                if 'result' in content:
                    result = content['result']
                    if 'intent' in result:
                        intent = result['intent']
                        if 'service' in intent:
                            # 如果是动作服务
                            if intent['service'] == 'YUJIAO.robot_action':
                                semantic = intent['semantic'][0]
                                action_intent = semantic['intent']
                                if action_intent == 'general_action':
                                    normValue = []
                                    # 遍历动作部位
                                    for values in semantic['slots']:
                                        value = values['normValue']
                                        normValue.append(value)
                                    print('position:')
                                    print(normValue)
                                    return normValue
                                else:
                                    return None
                            # 如果是问答
                            elif 'answer' in intent:
                                return intent['answer']['text']
                            else:
                                return None
                        else:
                            return None
                    else:
                        return None
                else:
                    return None
            else:
                return None
        else:
            return None
    else:
        return None


# 解析语音开始点



# print(parse_action(text))
'''
result = json.loads(text,encoding='UTF=8')
print(result.keys())
#print(result.values())
semantic = result['semantic']
intent = semantic[0]['intent']
print('intent:'+intent)
normValue = semantic[0]['slots'][0]['normValue']
print('normValue:'+normValue)
#print('rc:'+str(result['rc'])
'''
